#include "AnaDef.h"

TString fTag;
void ini(const TString fin, const Int_t kcen)
{
  fTag = fin;
  fTag.ReplaceAll("10-d-e","0de.");

  fTag.ReplaceAll("list.","");
  fTag=fTag(0, fTag.First('.'));
  if(fTag.Contains("_")) 
    fTag=fTag(0, fTag.First('_'));

  if(!fTag.Contains("MC"))
    fTag+="DATA";

  if(kcen!=-999)
    fTag+=Form("CEN%d", kcen);
}

void printOut(const TString vname, const Double_t var)
{
  printf("const Double_t %s%s = %.0f;\n", vname.Data(), fTag.Data(), var);
}

int main(int argc, char * argv[])
{
  for(int ii=0; ii<argc; ii++){
    printf("%d: %s\n", ii, argv[ii]);
  }

  Int_t kcen = -999;

  if(argc!=2){
    if(argc!=3){
      printf("argc!=2 && !=3\n");return 1;
    }
    kcen = atoi(argv[2]);
  }

  ini(argv[1], kcen);
  printf("ini fTag %s\n", fTag.Data());

  //============================================

  ifstream fin(argv[1]);
  TString buff;
  //[ii]: corresponds to the fill value Fill(ii)
  const Int_t nbit = 31;
  TString vname[nbit];
  Double_t vars[nbit];
  for(Int_t ifill=0; ifill<nbit; ifill++){
    vname[ifill]="";
    vars[ifill]=0;
  }

  vname[0]="PsRawN"; //should = PsCutinputN; 1
  vname[1]="PsCutinputN"; //should = "PsRawN"; 10
  vname[3]="PsCutinputCuteventN";//should = "PsPrimRecVz"; 13

  vname[10]="PsN";
  vname[11]="PsPrimN";//should = PsN; 10
  vname[12]="PsVrecN";
  vname[13]="PsVrecVzN";

  while(fin.good()){
    fin>>buff;
    if(buff!=""){
      TFile *ff = new TFile(buff);
      if(!ff){
        printf("%s no file!!\n", buff.Data()); continue;
      }

      TList *ll = (TList*)ff->Get("xlu_list");

      TTree * tt = (TTree*)ff->Get("tree");
      if(!tt || !ll){
        printf("%s no list tree!\n", buff.Data()); gDirectory->ls();
        ff->Close();
        delete ff;
        continue;
      }
      const Double_t ntrk = tt->GetEntries();

      TH2D * hcuts = (TH2D*) ll->FindObject("cuts");
      if(!hcuts){
        printf("%s no hcuts!\n", buff.Data()); exit(1);
      }

      for(Int_t ifill=0; ifill<31; ifill++){
        if(vname[ifill].Length()==0)
          continue;

        if(kcen<0){
          vars[ifill]+= hcuts->Integral(ifill+1, ifill+1, 0, hcuts->GetNbinsY()+1);
        }
        else{
          vars[ifill]+= hcuts->Integral(ifill+1, ifill+1, kcen+1, kcen+1);
        }

        const Double_t rat = ntrk/vars[ifill];
        printf("rat %d %s %e %e %e,\n", ifill, argv[1], vars[ifill], ntrk, rat);
      }

      ff->Close();
      delete ff;
    }
  }

  printf("%s Nevent PsVrecVzN: %.0f\n", argv[1], vars[13]);

  /*
    root [0] TH1D * hh=new TH1D("vtxZ","",500, -50,50);
    root [3] hh->GetXaxis()->GetBinLowEdge(201)
    (const Double_t)(-1.00000000000000000e+01)
    root [4] hh->GetXaxis()->GetBinUpEdge(300)
    (const Double_t)1.00000000000000000e+01

  const Double_t inrange = gv->Integral(201,300);
  const Double_t allrange = gv->Integral(0, gv->GetNbinsX()+1);
  const Double_t vtxAcc = inrange/allrange;
  */

  if(kcen<0){
    if( vars[0]!=vars[1] || vars[10]!=vars[1]){
      printf("bad a %.0f %.0f, %.0f %.0f\n",vars[0], vars[1], vars[10], vars[1]);
      return 1;
    }  
  }

  if(vars[13]!=vars[3] || vars[11]!=vars[10] ){
    printf("bad b %.0f %.0f, %.0f %.0f\n", vars[13], vars[3], vars[11], vars[10]); 
    return 1;
  }  

  for(Int_t ifill=0; ifill<31; ifill++){
    if(ifill==0 || ifill==1 || ifill==3 || ifill==11){
      continue;
    }

    if(vname[ifill].Length()==0)
      continue;

    printOut(vname[ifill], vars[ifill]);
  }

  return 0;
}
